package com.example.drive.car.service.impl;

import com.example.drive.car.mapper.CarMapper;
import com.example.drive.car.pojo.Car;
import com.example.drive.car.pojo.CarDTO;
import com.example.drive.car.service.CarService;
import com.example.drive.coach.mapper.CoachMapper;
import com.example.drive.coach.pojo.entity.Coach;
import com.example.drive.common.ServiceCode;
import com.example.drive.common.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CoachMapper coachMapper;
    /**
     * 获取车辆列表
     * @return
     */
    @Override
    public List<Car> getList() {
        List<Car> carList = carMapper.getList();
        if (carList == null) {
            throw new ServiceException(ServiceCode.ERR_EXISTS, "不存在车辆！");
        }
        return carList;
    }

    /**
     * 添加车辆
     * @param carDTO
     */
    @Override
    public void add(CarDTO carDTO) {
        int result = carMapper.add(carDTO);
        if (result <= 0) {
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "添加失败！");
        }
    }

    /**
     * 删除车辆
     * @param id
     */
    @Override
    public void delete(Integer id) {
        Car car = carMapper.getById(id);
        if (car == null) {
            throw new ServiceException(ServiceCode.ERR_EXISTS, "不存在车辆！");
        }
        int result = carMapper.delete(id);
        if (result <= 0) {
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "删除失败！");
        }
    }

    /**
     * 更新车辆信息
     * @param carDTO
     */
    @Override
    public void update(CarDTO carDTO, Integer id) {
        Car car = carMapper.getById(id);
        if (car == null) {
            throw new ServiceException(ServiceCode.ERR_EXISTS, "不存在车辆！");
        }
        car.setName(carDTO.getName());
        car.setType(carDTO.getType());
        car.setBrand(carDTO.getBrand());
        car.setNewlevel(carDTO.getNewlevel());
        car.setOptype(carDTO.getOptype());

        int result = carMapper.update(car);
        if (result <= 0) {
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "更新失败！");
        }
    }

    /**
     * 上传图片
     * @param id
     * @param image
     */
    @Override
    public void uploadImage(Integer id, MultipartFile image) {
        String uploadDirectory = "D:/vue-program/driving-school/driving/public/image";

        try {
            if (image.isEmpty()) {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "请选择一个非空文件。");
            }

            // 创建上传目录，如果不存在
            Path uploadPath = Paths.get(uploadDirectory);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 文件存储
            String fileName = id + "_" + image.getOriginalFilename();
            Path targetPath = uploadPath.resolve(fileName);
            Files.copy(image.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
            //保存地址
            Car car = carMapper.getById(id);
            car.setImage(targetPath.toString());
            int result = carMapper.saveImage(car);
            if (result <= 0) {
                throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "保存失败！");
            }
        } catch (Exception e) {
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "文件上传失败 - " + e.getMessage());
        }
    }

    /**
     * 查询车辆
     * @param id
     * @return
     */
    @Override
    public Car search(Integer id) {
        Car car = carMapper.getById(id);
        if (car == null) {
            throw new ServiceException(ServiceCode.ERR_EXISTS, "不存在该车辆");
        }
        return car;
    }

    /**
     * 分配车辆
     * @param id
     * @param coach_id
     */
    @Override
    public void arrange(Integer id,Integer coach_id){
        Car car = carMapper.getById(id);
        if (car == null) {
            throw new ServiceException(ServiceCode.ERR_EXISTS, "不存在该车辆");
        }
        Coach coach = coachMapper.getById(coach_id);
        if(coach == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该教练！");
        }
        car.setCoach_id(coach_id);
        int result = carMapper.update(car);
        if (result <= 0) {
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "更新失败！");
        }
    }

}